﻿using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPressure
{
    public partial class EditTemplateForm: BaseForm
    {
        #region 属性
        private Template obj = new Template();

        public int template_number;
        #endregion

        #region 自定义方法
        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["template_number"] = TemplateNumberInput.Text;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            int count = list.Count;
            if (count > 0)
            {
                AntdUI.Message.error(this, where["template_number"] + "模板已存在", Font);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>格式化判断方式
        /// 
        /// </summary>
        /// <param name="estimate"></param>
        /// <returns></returns>
        private int FormatEstimate(string str)
        {
            if (str == "固定数值")
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        private string EstimateValueToInt(int value)
        {
            if (value == 1)
            {
                return "浮动范围"; 
            }
            else
            {
                return "固定数值";
            }
        }

        /// <summary>根据编号查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Template TemplateWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["template_number"] = number;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckSelectedRowExist(int number)
        {
            obj = TemplateWithNumber(number);
            if (obj == null)
            {
                Alert.Error("模板记录不存在", this);
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion

        #region 系统方法
        public EditTemplateForm()
        {
            InitializeComponent();
        }

        private void EditTemplateForm_Load(object sender, EventArgs e)
        {
            if (template_number > 0)
            {
                bool res = CheckSelectedRowExist(template_number);
                if (!res)
                {
                    this.Close();
                    Alert.Error("模板记录不存在", this.Owner);
                    return;
                }
                TemplateNumberInput.Text = obj.template_number.ToString();
                PipePositionSelected.Text = obj.pipe_position;
                DesignPressureInputNumber.Text = obj.design_pressure.ToString();
                string edit = "否";
                if (obj.edit_enable == 1)
                {
                    edit = "是";
                }
                EditEnableSelect.Text = edit;

                FirstStagePressureInput.Text = obj.first_stage_pressure.ToString();
                FirstStageMinuteInput.Text = obj.first_stage_minute.ToString();
                FirstStageEstimateSelect.Text = EstimateValueToInt(obj.first_stage_estimate.ObjToInt());
                FirstStageEstimateValueInput.Text = obj.first_stage_estimate_value.ToString();

                SecondStagePressureInput.Text = obj.second_stage_pressure.ToString();
                SecondStageMinuteInput.Text = obj.second_stage_minute.ToString();
                SecondStageEstimateSelect.Text = EstimateValueToInt(obj.second_stage_estimate.ObjToInt());
                SecondStageEstimateValueInput.Text = obj.second_stage_estimate_value.ToString();

                ThirdStagePressureInput.Text = obj.third_stage_pressure.ToString();
                ThirdStageMinuteInput.Text = obj.third_stage_minute.ToString();
                ThirdStageEstimateSelect.Text = EstimateValueToInt(obj.third_stage_estimate.ObjToInt());
                ThirdStageEstimateValueInput.Text = obj.third_stage_estimate_value.ToString();

                RigorStagePressureInput.Text = obj.rigor_stage_pressure.ToString();
                RigorStageMinuteInput.Text = obj.rigor_stage_minute.ToString();
                RigorStageEstimateSelect.Text = EstimateValueToInt(obj.rigor_stage_estimate.ObjToInt());
                RigorStageEstimateValueInput.Text = obj.rigor_stage_estimate_value.ToString();

                IntensityStagePressureInput.Text = obj.intensity_stage_pressure.ToString();
                IntensityStageMinuteInput.Text = obj.intensity_stage_minute.ToString();
                IntensityStageEstimateSelect.Text = EstimateValueToInt(obj.intensity_stage_estimate.ObjToInt());
                IntensityStageEstimateValueInput.Text = obj.intensity_stage_estimate_value.ToString();
            }
        }
        #endregion

        #region 点击按钮
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (TemplateNumberInput.Text.Length == 0)
            {
                Alert.Error("请输入模板编号", this);
                TemplateNumberInput.Focus();
                return;
            }

            if (PipePositionSelected.Text.Length == 0)
            {
                Alert.Error("请选择管道位置", this);
                TemplateNumberInput.Focus();
                return;
            }

            if (DesignPressureInputNumber.Text.Length == 0)
            {
                Alert.Error("请输入设计压力", this);
                TemplateNumberInput.Focus();
                return;
            }

            if (EditEnableSelect.Text.Length == 0)
            {
                Alert.Error("请选择是否允许编辑", this);
                TemplateNumberInput.Focus();
                return;
            }
            
            obj.template_number = TemplateNumberInput.Text.ObjToInt();
            obj.pipe_position = PipePositionSelected.Text;
            obj.design_pressure = float.Parse(DesignPressureInputNumber.Text);
            int edit = 0;
            if (EditEnableSelect.Text == "是")
            {
                edit = 1;
            }
            obj.edit_enable = edit;

            obj.first_stage_pressure = float.Parse(FirstStagePressureInput.Text);
            obj.first_stage_minute = float.Parse(FirstStageMinuteInput.Text);
            obj.first_stage_estimate = FormatEstimate(FirstStageEstimateSelect.Text);
            obj.first_stage_estimate_value = float.Parse(FirstStageEstimateValueInput.Text);

            obj.second_stage_pressure = float.Parse(SecondStagePressureInput.Text);
            obj.second_stage_minute = float.Parse(SecondStageMinuteInput.Text);
            obj.second_stage_estimate = FormatEstimate(SecondStageEstimateSelect.Text);
            obj.second_stage_estimate_value = float.Parse(SecondStageEstimateValueInput.Text);

            obj.third_stage_pressure = float.Parse(ThirdStagePressureInput.Text);
            obj.third_stage_minute = float.Parse(ThirdStageMinuteInput.Text);
            obj.third_stage_estimate = FormatEstimate(ThirdStageEstimateSelect.Text);
            obj.third_stage_estimate_value = float.Parse(ThirdStageEstimateValueInput.Text);

            obj.rigor_stage_pressure = float.Parse(RigorStagePressureInput.Text);
            obj.rigor_stage_minute = float.Parse(RigorStageMinuteInput.Text);
            obj.rigor_stage_estimate = FormatEstimate(RigorStageEstimateSelect.Text);
            obj.rigor_stage_estimate_value = float.Parse(RigorStageEstimateValueInput.Text);

            obj.intensity_stage_pressure = float.Parse(IntensityStagePressureInput.Text);
            obj.intensity_stage_minute = float.Parse(IntensityStageMinuteInput.Text);
            obj.intensity_stage_estimate = FormatEstimate(IntensityStageEstimateSelect.Text);
            obj.intensity_stage_estimate_value = float.Parse(IntensityStageEstimateValueInput.Text);
            List<Template> list = new List<Template>();
            list.Add(obj);
            int res = SqlSugarHelper.Updateable<Template>(list);
            if (res > 0)
            {
                TemplateManageForm frm = (TemplateManageForm)this.Owner;
                frm.LoadTemplateList();
                this.Close();
                Alert.Info("编辑模板成功", this.Owner);
            }
        }
        #endregion

    }
}
